# -*- coding:utf-8 -*-
import redis

from app_config import REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD


# 数据库的连接池

class BaseRedis:
    def __init__(self):
        self.REDIS_HOST = REDIS_HOST
        self.REDIS_PORT = REDIS_PORT
        self.REDIS_DB = REDIS_DB
        self.REDIS_PASSWORD = REDIS_PASSWORD
        if self.REDIS_PASSWORD:
            self.db_conn_pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, password=REDIS_PASSWORD)
        else:
            self.db_conn_pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)
        self.con_ = redis.Redis(connection_pool=self.db_conn_pool)
    
    def get_list_of_key(self, key):
        """
        根据key获取value
        :param key:
        :return:
        """
        return self.con_.lrange(key,0.-1)
    
    def sadd(self, key, value, end_time=None):
        r = self.con_
        r.sadd(key, value)
        if end_time is not None:
            print "-----end_time is not None, end_time=" + str(end_time)
            r.expire(key, end_time)

    def smembers(self, key):
        """
        根据key获取value
        :param key:
        :return:
        """
        return self.con_.smembers(key)
    
    def sismember(self, key, value):
            """
            根据key获取value
            :param key:
            :return:
            """
            return self.con_.sismember(key, value)

    def set_cache_key_value(self, key, value, end_time=None):
        """
        设置通用型存储缓存方法
        :param key:
        :param value:
        :param end_time:
        :return:
        """
        print "=key=", key
        print "=value=", value
        r = self.con_
        r.set(key, value)
        if end_time is not None:
            print "-----end_time is not None, end_time=" + str(end_time)
            r.expire(key, end_time)
    
    def get_cache_key_value(self, key):
        """
        根据key获取value
        :param key:
        :return:
        """
        return self.con_.get(key)
    
    def delete_cache_key(self, key):
        """
        删除key
        :param key:
        :return:
        """
        return self.con_.delete(key)

    def set_cache_hash_key_value(self, key, data):
        """
        设置通用型存储缓存方法
        :param key:
        :param data:
        :return:
        """
        self.con_.hmset(key, data)

    def get_hash_cache_key_value(self, key):
        """
        根据key获取value
        :param key:
        :return:
        {
            u'info': {
                'appKey': '143127cc5fe819d41fe2583590b76e8e',
                u'accid': u'ebcb0daca1a411e7a3f900163e0d4004',
                u'token': u'189ce6a80e300d7e067217a1b838c005'
            },
            u'code': 200
        }
        """
        return self.con_.hmget(key, "code", "info")

    def set_cache_key_value_pipeline(self, key, value):
        """
        设置通用型存储缓存方法,
        如果在 WATCH 执行之后, EXEC 执行之前, 有其他客户端修改了key的值, 那么当前客户端的事务就会失败.
        :param key:
        :param value:
        :return:
        """
        r = self.con_
        pipe = r.pipeline(transaction=True)
        try:
            pipe.watch(key)
            pipe.multi()
            pipe.set(key, value)
            pipe.execute()
            return True
        except redis.exceptions.WatchError, ex:
            print ex
            pipe.unwatch()
            return False
    
    def avoid_repeat_request(self, key, expire_time):
        """redis防止重复请求"""
        if self.con_.incr(key) == 1:
            return self.con_.expire(key, expire_time)
        return False
    
    def keys(self):
        return self.con_.keys()
    
    def flushall(self):
        return self.con_.flushall()


con_redis = BaseRedis()
